﻿Public Class FrmNhomNhaCungCap
    Dim provider As New DataProvider
    Dim flag As String = 0
    Function LoadDuLieu()
        dtgnhomNCC.DataSource = New DataTable
        Dim sql As String
        sql = "select MaNhomNCC,TenNhomNCC from NhomNCC"
        Dim dt As New DataTable
        dt = provider.ExecuteQuery(sql)
        dtgnhomNCC.DataSource = dt
        Enable(False)
        btnthem.Enabled = True
        btnluu.Enabled = False
        btnsua.Enabled = True
        btnxoa.Enabled = True
        dtgnhomNCC.Enabled = True
        btnhuy.Enabled = False


    End Function
    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        flag = 1
        Enable(True)
        btnsua.Enabled = False
        btnxoa.Enabled = False
        btnthem.Enabled = False
        dtgnhomNCC.Enabled = False
        txtmaNCC.Text = ""
        txttenNCC.Text = ""
        btnhuy.Enabled = True
    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        Try
            Dim row As Integer = dtgnhomNCC.CurrentCell.RowIndex
            Dim MaNhomNCC As String
            MaNhomNCC = dtgnhomNCC.Rows(row).Cells(0).Value
            Dim sql As String
            sql = " DELETE FROM NhomNCC where MaNhomNCC = '" + MaNhomNCC + "'"
            provider.ExecuteNonQuery(sql)
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        btnhuy.Enabled = False
        If txtmaNCC.Text = "" Then
            MessageBox.Show("Chọn 1 nhà cung cấp để sửa ")
        Else
            flag = 2
            Enable(True)
            btnthem.Enabled = False
            btnxoa.Enabled = False
            btnsua.Enabled = False
            txtmaNCC.Enabled = False

        End If
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try

            If flag = 1 Then
                Dim MaNhomNCC As String
                Dim TenNhomNCC As String
                MaNhomNCC = txtmaNCC.Text
                TenNhomNCC = txttenNCC.Text
                Dim sql As String
                sql = "select * from NhomNCC where MaNhomNCC='" + MaNhomNCC + "'"
                Dim dt As New DataTable
                dt = provider.ExecuteQuery(sql)

                If dt.Rows.Count > 0 Then
                    MessageBox.Show("Mã nhà cung cấp Trùng")
                    txtmaNCC.Focus()
                Else
                    sql = "insert into NhomNCC values(N'" + MaNhomNCC + "',N'" + TenNhomNCC + "')"
                    provider.ExecuteNonQuery(sql)
                    LoadDuLieu()
                End If


            ElseIf flag = 2 Then

                Dim MaNhomNCC As String
                Dim TenNhomNCC As String
                MaNhomNCC = txtmaNCC.Text
                TenNhomNCC = txttenNCC.Text
                Dim sql As String
                sql = "update NhomNCC set MaNhomNCC = '" + MaNhomNCC + "', TenNhomNCC= N'" + TenNhomNCC + "' WHERE MaNhomNCC = '" + MaNhomNCC + "'"
                provider.ExecuteNonQuery(sql)
                LoadDuLieu()
            End If

        Catch ex As Exception
            MessageBox.Show("Lỗi")
        End Try
    End Sub
    Private Sub Enable(ByVal i As Boolean)
        txtmaNCC.Enabled = i
        txttenNCC.Enabled = i
        btnluu.Enabled = i

    End Sub
    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        LoadDuLieu()
    End Sub

    Private Sub FrmNhomNhaCungCap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()
    End Sub

    Private Sub dtgnhomNCC_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgnhomNCC.CellClick
        Try
            Dim row As Integer = dtgnhomNCC.CurrentCell.RowIndex
            txtmaNCC.Text = dtgnhomNCC.Rows(row).Cells(0).Value
            txttenNCC.Text = dtgnhomNCC.Rows(row).Cells(1).Value
        Catch ex As Exception

        End Try
    End Sub
End Class